package org.kernely.test.log;

import org.kernely.ui.mvp.IKernelyView;
import org.osgi.service.log.LogService;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class LogView extends GridLayout implements IKernelyView{
	/**
	 * The Serial Version ID
	 */
	private static final long serialVersionUID = 454242724587554864L;
	
	public static final Class<? extends IKernelyView> ID_VIEW = LogView.class;
	private TextField testLog;
	private Panel northPanel;
	private Panel southPanel;
	private Panel eastPanel;
	private Table logTable;
	private HorizontalLayout checkLayout;
	private VerticalLayout westLayout;
	private CheckBox checkInfo;
	private CheckBox checkDebug;
	private CheckBox checkWarning;
	private CheckBox checkError;
	private LogButton addInfo;
	private LogButton addDebug;
	private LogButton addWarning;
	private LogButton addError;
	private Button refreshButton;

	/**
	 * LogView's Constructor
	 */
	public LogView(){
		
		checkLayout = new HorizontalLayout();

		this.checkInfo = new LogCheckBox("Display \"INFO\" logs", false); //We don't display info logs by default, to start quickly the page (log info are numerous)
		this.checkDebug = new LogCheckBox("Display \"DEBUG\" logs", true);
		this.checkWarning = new LogCheckBox("Display \"WARNING\" logs", true);
		this.checkError = new LogCheckBox("Display \"ERROR\" logs", true);
		addInfo = new LogButton(LogService.LOG_INFO);
		addDebug = new LogButton(LogService.LOG_DEBUG);
		addWarning = new LogButton(LogService.LOG_WARNING);
		addError = new LogButton(LogService.LOG_ERROR);
		refreshButton = new Button("Refresh logs");
		
		testLog = new TextField("Add a log");
		northPanel = new Panel();
		southPanel = new Panel();
		eastPanel = new Panel();
		westLayout = new VerticalLayout();
		
		this.setColumns(4);
		this.setRows(1);
		westLayout.addComponent(northPanel);
		westLayout.addComponent(southPanel);

		this.addComponent(westLayout);
		this.addComponent(eastPanel,1,0,3,0);

		eastPanel.addComponent(checkLayout);
		checkLayout.addComponent(checkInfo);
		checkLayout.addComponent(checkDebug);
		checkLayout.addComponent(checkWarning);
		checkLayout.addComponent(checkError);
		checkLayout.addComponent(refreshButton);


		logTable = new Table();

		logTable.setSizeFull();
		logTable.setPageLength(0);
		logTable.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		logTable.addContainerProperty("Date", String.class, null);
		logTable.addContainerProperty("Log level", String.class,  null);
		logTable.addContainerProperty("Logged by", String.class, null);
		logTable.addContainerProperty("Message",  String.class,  null);
		
		logTable.setColumnWidth("Log level", 70);
		logTable.setColumnWidth("Date", 130);
		logTable.setColumnWidth("Logged by", 120);
		
		southPanel.addComponent(new Label("Step 1 : Add a new Log message in the field above."));
		southPanel.addComponent(new Label("Step 2 : Click on one of the four buttons, to save the message as an info, debug, warning or error log."));
		southPanel.addComponent(new Label("Step 3 : Click on the \"Refresh Button\" to see the log you entered in the table."));
		
		northPanel.addComponent(testLog);
		northPanel.addComponent(addInfo);
		northPanel.addComponent(addDebug);
		northPanel.addComponent(addWarning);
		northPanel.addComponent(addError);
		
		eastPanel.addComponent(logTable);
		
		
		this.setSizeFull();
	}

	private class LogCheckBox extends CheckBox{
		private static final long serialVersionUID = -5754594621339389249L;
		public LogCheckBox(String caption,boolean defaultValue){
			this.setCaption(caption);
			this.setValue(defaultValue);
			this.setImmediate(true);

		}
	}
	
	/**
	 * LogButton class, redefinition of Button class
	 */
	class LogButton extends Button{
		/**
		 * The Serial Version ID
		 */
		private static final long serialVersionUID = -5521046189715199866L;
		private int logLevel;
		private String typeOfLog = null;
		/**
		 * LogButton's Constructor
		 * @param logLevel : INFO, ERROR, WARNING,DEBUG
		 */
		public LogButton(int logLevel) {
			this.logLevel = logLevel;
			switch(logLevel) {
			case LogService.LOG_DEBUG:
				typeOfLog = "Log as Debug";
				break;
			case LogService.LOG_INFO:
				typeOfLog = "Log as Info";
				break;
			case LogService.LOG_ERROR:
				typeOfLog = "Log as Error";
				break;
			case LogService.LOG_WARNING:
				typeOfLog = "Log as Warning";
				break;
			}
			this.setCaption(typeOfLog);
		}
		/**
		 * Retrieve the log level associated to the button
		 * @return the log level value
		 */
		public int getLogLevel(){
			return this.logLevel;
		}
	}

	/**
	 * @see org.kernely.core.service.IKernelyView#getComponent()
	 */
	@Override
	public final Component getComponent() {
		return this;
	}


	//Getters and setters
	/**
	 * Gets the checkInfo
	 * @return the checkInfo component
	 */
	public final CheckBox getCheckInfo() {
		return checkInfo;
	}

	/**
	 * Gets the checkDebug component
	 * @return the checkDebug
	 */
	public final CheckBox getCheckDebug() {
		return checkDebug;
	}

	/**
	 * Gets the checkWarning component
	 * @return the checkWarning
	 */
	public final CheckBox getCheckWarning() {
		return checkWarning;
	}

	/**
	 * Gets the checkError component
	 * @return the checkError
	 */
	public final CheckBox getCheckError() {
		return checkError;
	}

	/**
	 * Gets the addInfo button
	 * @return the addInfo
	 */
	public final LogButton getAddInfo() {
		return addInfo;
	}

	/**
	 * Gets the addDebug button
	 * @return the addDebug
	 */
	public final LogButton getAddDebug() {
		return addDebug;
	}

	/**
	 * Gets the addWarning button
	 * @return the addWarning
	 */
	public final LogButton getAddWarning() {
		return addWarning;
	}

	/**
	 * Gets the addError button
	 * @return the addError
	 */
	public final LogButton getAddError() {
		return addError;
	}

	/**
	 * Gets the refreshButton
	 * @return the refreshButton
	 */
	public final Button getRefreshButton() {
		return refreshButton;
	}


	/**
	 * Gets the testLog
	 * @return the testLog
	 */
	public final TextField getTestLog() {
		return testLog;
	}


	/**
	 * Gets the logTable
	 * @return the logTable
	 */
	public final Table getLogTable() {
		return logTable;
	}
	
	

	
}
